package com.example.lancooznbk.view.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.example.lancooznbk.view.widget.EmptyMainLayout;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.trello.rxlifecycle.components.support.RxFragment;

/**
 * Created by tangminglong on 17/7/26.
 */

public abstract class BaseFragment extends RxFragment implements IBaseView ,EmptyMainLayout.OnRetryListener{

    private BaseActivity mActivity;

    @Nullable
    protected EmptyMainLayout mEmptyViewLayout;

    //缓存Fragment view
    private View mRootView;
    private boolean mIsMulti = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mActivity = (BaseActivity) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(attachLayoutRes(), null);
            //mEmptyMainLayout = mRootView.findViewById(R.id.empty_layout);
            initViews(mRootView, savedInstanceState);
        }


        ViewGroup parent = (ViewGroup) mRootView.getParent();
        if (parent != null){
            parent.removeView(mRootView);
        }

        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getUserVisibleHint()  && mRootView != null && !mIsMulti) {
            mIsMulti = true;
            updateViews(false);
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        //  String className = getClass().getSimpleName();
        if (isVisibleToUser && isVisible() && mRootView != null && !mIsMulti){
            mIsMulti = true;
            updateViews(true);
        }else {
            super.setUserVisibleHint(isVisibleToUser);
        }
    }

    /**
     * 更新布局
     * @param isShowLoading 是否显示加载中的布局
     */
    protected abstract void updateViews(boolean isShowLoading);

    /**
     * 加载布局
     * @return
     */
    public abstract int attachLayoutRes();

    /**
     * 初始化布局
     * @param view
     * @param savedInstanceState
     */
    public abstract void initViews(View view, Bundle savedInstanceState);

    @Override
    public BaseActivity getHolderActivity() {
        return mActivity;
    }

    @Override
    public void showLoading() {
        if (mEmptyViewLayout != null) {
            mEmptyViewLayout.setEmptyStatus(EmptyMainLayout.STATUS_LOADING);
        }
    }

    @Override
    public void hideLoading() {
        if (mEmptyViewLayout != null) {
            mEmptyViewLayout.hide();
        }
    }

    @Override
    public void hideLoading(final HideLoadingFinished hideLoadingFinished) {
        hideLoading();
        mEmptyViewLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                hideLoadingFinished.hideFinished();
                mEmptyViewLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }

    @Override
    public void showNetError() {
        if (mEmptyViewLayout != null) {
            mEmptyViewLayout.setEmptyStatus(EmptyMainLayout.STATUS_NO_NET);
            mEmptyViewLayout.setRetryListener(this);
        }
    }

    @Override
    public void showNoData(String msg) {
        if (mEmptyViewLayout != null) {
            mEmptyViewLayout.setEmptyStatus(EmptyMainLayout.STATUS_NO_DATA);
            mEmptyViewLayout.setEmptyMessage(msg);
        }
    }

    @Override
    public void onRetry() {
        updateViews(true);
    }

    protected void initToolBar(Toolbar toolbar, boolean homeAsUpEnabled, int resTitle){
        mActivity.initToolBar(toolbar,homeAsUpEnabled,resTitle);
    }

    protected void initToolBar(Toolbar toolbar, boolean homeAsUpEnabled, String title){
        mActivity.initToolBar(toolbar,homeAsUpEnabled,title);
    }


    protected int getStatusBarHeight(){
         return mActivity.getStatusBarHeight();
    }

}
